/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.dbtools.tableditor;

import java.awt.*;
import javax.swing.*;

import jdbcmanager.app.App;
import jdbcmanager.dbtools.DbUtil;
import jdbcmanager.dbtools.datatypes.*;

/**
 * A panel containig a Columns (fields) of a Table and widgets to manipulate them.
 *
 * @author Francisco Morero Peyrona
 */

final class TabColumns extends JPanel
{
   private JxTableColumns tblColumns;   // A JTable that holds Table Columns

   //-------------------------------------------------------------------------//

   TabColumns()
   {
      this( null );
   }

   TabColumns( TableColumnDefinition[] aColumn )
   {
      this.tblColumns = new JxTableColumns();
      this.tblColumns.setColumns( aColumn );
      
      init();
   }

   TableColumnDefinition[] getColumns()
   {
      return this.tblColumns.getColumns();
   }
   
   boolean isCongruent()
   {
      boolean bSuccess = false;
      
      TableColumnDefinition[] aColumn = getColumns();
      
      if( aColumn.length > 0 )   // A table needs at least 1 column
      {
         bSuccess = true;
         
         for( int n = 0; n < aColumn.length; n++ )
            bSuccess = (bSuccess && DbUtil.getAgentDBMS().getAgentSQL().isValidColumn( aColumn[n] ));
         
         if( bSuccess )
         {  // Paso de mirar si el MaxRowSize inlcuye o no los BLOB: asumo q si y listo
            int nMaxRowSize = DbUtil.getAgentDBMS().getAgentMetaData().getMaxRowSize();
            int nRowSize    = 0;
            
            for( int n = 0; n < aColumn.length; n++ )
                nRowSize += aColumn[n].getSize();
            
            if( nRowSize > nMaxRowSize )
            	bSuccess = App.yesOrNoDialog( "Column problem", 
                                             "It looks like the column legth is bigger than maximum./"+ 
                                             "To continue should not damage anything but can report an error from the DBMS."+
                                             "Do you want to continue?" );
         }
      }
      
      return bSuccess;
   }
   
   //-------------------------------------------------------------------------//
   
   private void init()
   {
      setBorder( BorderFactory.createEmptyBorder( 8,8,8,8 ) );
      setLayout( new BorderLayout() );
      
      add( new JPanelParent( this.tblColumns ), BorderLayout.CENTER );
   }
}